// Copyright (c) 2009 Alistair Jones
// This program is made available under the terms of the MIT License.
package org.hypirinha.html.output.dom;

import static org.hypirinha.html.AttributeFactory.classs;
import static org.hypirinha.html.ElementFactory.div;
import org.hypirinha.html.elements.Div;
import org.hypirinha.html.output.DomAdapter;
import static org.hypirinha.html.output.dom.XPathEvaluator.NamespaceMapping.namespace;
import static org.hypirinha.html.output.dom.XPathEvaluator.xPath;
import org.hypirinha.html.output.features.HtmlNamespace;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.w3c.dom.Document;

public class NamespaceTest {

    @Test
    public void shouldIncludeNamespaceIfSupplied() throws Exception {
        Div content = div();
        content.div();

        Document document = new DomAdapter(HtmlNamespace.xhtml1).asDocument(content);

        assertFalse(xPath(document, namespace("xhtml", "http://www.w3.org/1999/xhtml")).bool("/div/div"));
        assertTrue(xPath(document, namespace("xhtml", "http://www.w3.org/1999/xhtml")).bool("/xhtml:div/xhtml:div"));
    }

    @Test
    public void shouldPutAttributesInTheCorrectNamespace() throws Exception {
        Div content = div(classs("important"));
        content.div();

        Document document = new DomAdapter(HtmlNamespace.xhtml1).asDocument(content);

        assertFalse(xPath(document, namespace("xhtml", "http://www.w3.org/1999/xhtml")).bool("/xhtml:div/@class"));
        assertTrue(xPath(document, namespace("xhtml", "http://www.w3.org/1999/xhtml")).bool("/xhtml:div/@xhtml:class"));
    }

}